﻿/// <reference path="../../View/DeptList.aspx" />
/// <reference path="../../View/DeptList.aspx" />
$(function () {
    var url = new Url();
    var $Id = url.GetQueryString("id");
    var $css = url.GetQueryString("css");
    //修改时绑定页面
    if ($Id) {
        $.getJSON("../Handler/DeptHandler.ashx",
            { Id: $Id, type: 4, _: Math.random() },
            function (result) {
                if (result.Status) {
                    $(".txtName").val(result.Data.DepName);
                    if (result.Data.ParentDepId) {
                        $(".txtDep").text(result.Data.PDepName);
                        $(".hdDeps").val(result.Data.ParentDepId);
                    }
                    else {
                        $(".txtDep").text("您还未选择上级部门");
                    }
                }
                else {
                    alert(result.Message);
                }
            });
    }
    //加载部门信息
    $(".btnSelDep").click(function () {
        $.getJSON("../Handler/DeptHandler.ashx", { type: 11, _: Math.random() }, function (result) {
            if (result.Status) {
                //递归获取当前部门的子部门
                var childDeptList = getChildDept(result.Data, $Id);

                //绑定jstree
                $('#jstree').jstree({
                    'core': { 'data': result.Data }
                });
                $('#jstree').on("ready.jstree", function (e, data) {
                    var idstr = $(".hdDeps").val();
                    if ($Id && idstr) {
                        var array = idstr.split(",");
                        for (var i = 0; i < array.length; i++) {
                            $.jstree.reference('#jstree').select_node(array[i]);
                        }
                    }
                });
                $('#jstree').on("changed.jstree", function (e, data) {
                    if (data.selected == $Id) {
                        alert("上级部门不能选为自己！");
                        return false;
                    }
                    else if ($.inArray(parseInt(data.selected[0]), childDeptList) != -1) {
                        alert("不能选择下级部门作为本部门上级!");
                        return false;
                    }
                    else {
                        $('.txtDep').text(data.instance.get_node(data.selected).text);
                        $(".hdDeps").val(data.selected);
                    }

                });
            }
            else {
                alert(result.Message);
            }
        });
    });
    //保存
    $(".btnSave").bind("click", function () {
        var $name = $(".txtName").val();
        var $pId = $("#hdDeps").val();
        if (!$name) {
            $(".txtName").focus();
            $(".errorName").text("部门不能为空");
            return;
        }
        if (!$pId || $pId == $Id) {
            $(".btnSelDep").click();
            return;
        }
        var data = { Name: $name, PId: $pId, type: $Id ? 2 : 1, _: Math.random() };
        if ($Id) {
            data.DepId = $Id;
        }
        $.getJSON("../Handler/DeptHandler.ashx",
            data,
            function (result) {
                if (result.Status) {
                    location.href = "DeptList.aspx?css=" + $css;
                } else {
                    alert(result.Message);
                }
            }
    )
    });
});

//递归获取子部门
function getChildDept(data, root) {
    var childDeptList = new Array();
    $.each(data, function (index, item) {
        if (item.parent == root) {
            if ($.inArray(item.id, childDeptList) == -1) {
                childDeptList.push(item.id);
            }
        }
    });
    var list = new Array();
    if (childDeptList.length > 0) {
        $.each(childDeptList, function (index, item) {
            $.merge(list, getChildDept(data, item));
        });
    }
    return $.merge(childDeptList, list);
}